package com.hesicare.adapter.worker;

import com.hesicare.adapter.service.*;
import com.hesicare.adapter.worker.pull.PullOnWorker;
import com.hesicare.adapter.worker.pull.PullSginOnWorker;
import com.hesicare.adapter.worker.pull.PullindicatorOnWorker;
import com.hesicare.health.entity.SysBloodGlucose;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.service.ISysPatientService;
import com.hesicare.health.worker.*;
import com.xxl.job.core.biz.model.ReturnT;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

import static com.hesicare.untils.exceptionUtils.exceptionutil.errorTrackSpace;

/**
 * @author 55097
 */
@Component
public class HisWorkManagers {

    protected final static Logger log = LoggerFactory.getLogger(HisWorkManagers.class);

    @Autowired
    private PullOnWorker pullOnWorker;

    @Autowired
    private PullindicatorOnWorker pullindicatorOnWorker;

    @Autowired
    private PullSginOnWorker pullSginOnWorker;

    @Autowired
    private DoBloodGlucose doBloodGlucose;


    @Autowired
    private ISysPatientService ISysPatientService;

    @Autowired
    private IParseIndicatorService parseIndicatorService;

    @Autowired
    private IParseDeptService parseDeptService;

    @Autowired
    private IParsePatientService parsePatientService;

    @Autowired
    private IParseUserService parseUserService;

    @Autowired
    private IParseAdviceService parseAdviceService;

    @Autowired
    private IParseSginImageService parseSginImageService;


    /**
     * 获取在院患者
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getPatient(ReturnT<String> returnT, String param) {
        try {
            parsePatientService.parsePatient((pullOnWorker.readPatientList(param)));
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("导入患者错误" + errorTrackSpace(e));
        }
        return returnT;
    }

    /**
     * 获取出院患者
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getOutPatient(ReturnT<String> returnT, String param) {
        try {
            parsePatientService.parseOutPatient(pullOnWorker.readOutPatientList(param));
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("导入出院患者错误" + errorTrackSpace(e));
        }
        return returnT;
    }

    /**
     * 清除多余患者
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getClearPatient(ReturnT<String> returnT, String param) {
        Set<String> clearPatientSet = new HashSet<>();
        try {
            pullOnWorker.readClearPatientList(clearPatientSet);
            parsePatientService.parseClearPatient(clearPatientSet);
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("清理患者错误" + errorTrackSpace(e));
        }
        return returnT;
    }

    /**
     * 获取员工
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getStaff(ReturnT<String> returnT, String param) {
        try {
            parseUserService.parseUser(pullOnWorker.readUserList(param));
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("导入员工错误" + errorTrackSpace(e));
        }
        return returnT;
    }


    /**
     * 获取机构列表
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getDept(ReturnT<String> returnT, String param) {
        try {
            parseDeptService.parseDept(pullOnWorker.readDeptList(param));
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("导入机构错误" + errorTrackSpace(e));
        }
        return returnT;
    }

    /**
     * 获取医嘱
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getOther(ReturnT<String> returnT, String param) {
        try {
            parseAdviceService.parseAdvice(pullOnWorker.readPotherList(param));
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT.setMsg("导入医嘱错误" + errorTrackSpace(e));
            returnT = ReturnT.FAIL;
        }
        return returnT;
    }

    /**
     * 回传血糖数据
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getRecv(ReturnT<String> returnT, String param) {
        List<SysBloodGlucose> obList = null;
        Map<Long, Boolean> resultMap = new HashedMap();
        try {
            obList = doBloodGlucose.getBloodGlucose();
            if (obList != null) {
                for (SysBloodGlucose sysBloodGlucose : obList) {
                    boolean status = pullOnWorker.recv(sysBloodGlucose);
                    resultMap.put(sysBloodGlucose.getId(), status);
                }
            }
            doBloodGlucose.updateBloodGlucose(resultMap);
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("回传数据错误" + errorTrackSpace(e));
        }
        return returnT;
    }

    /**
     * 获取电子签名图片保存至指定路径
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getSignImage(ReturnT<String> returnT, String param) {
        try {
            parseSginImageService.parseSginImage(pullSginOnWorker.readSign(param));
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("导入电子签名错误" + errorTrackSpace(e));
        }
        return returnT;
    }

    /**
     * 获取实验室检验检查
     *
     * @param returnT 处理结果对象
     * @param param   前端输入内容
     * @return 处理结果
     * @author: 55097
     * @date: 2022/1/4
     */
    public ReturnT<String> getIndicator(ReturnT<String> returnT, String param) {
        List<SysPatient> patientList = ISysPatientService.selectHospitalNumByHospitalType();
        try {
            patientList.stream().forEach(patient -> {
                parseIndicatorService.parseIndicator(pullindicatorOnWorker.readIndcatorList(patient));
            });
            returnT = ReturnT.SUCCESS;
        } catch (Exception e) {
            returnT = ReturnT.FAIL;
            returnT.setMsg("实验室检查错误" + errorTrackSpace(e));
        }
        return returnT;
    }
}
